package dbb

import (
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/widget"
	"github.com/gogf/gf/v2/database/gdb"
	octicons "github.com/lusingander/fyne-octicons"
)

func GenStruct(newDb gdb.DB, currentTbl string,
	tabs *container.DocTabs, msg *widget.Label) {
	if currentTbl == "" {
		msg.SetText("Please choose a table")
		return
	}
	genBool := binding.NewBool()
	fs, _ := newDb.Model().TableFields(currentTbl)
	entry := widget.NewMultiLineEntry()
	gJsonSupport := widget.NewCheck("GJsonSupport", func(b bool) {
		genBool.Set(b)
	})
	stdTime := widget.NewCheck("StdTime", func(b bool) {
		genBool.Set(b)
	})
	descriptionTag := widget.NewCheck("DescriptionTag", func(b bool) {
		genBool.Set(b)
	})
	noJsonTag := widget.NewCheck("NoJsonTag", func(b bool) {
		genBool.Set(b)
	})
	noModelComment := widget.NewCheck("NoModelComment", func(b bool) {
		genBool.Set(b)
	})
	selJsonCase := widget.NewSelect([]string{"CamelLower", "Camel", "Snake",
		"SnakeScreaming", "SnakeFirstUpper", "Kebab", "KebabScreaming"},
		func(s string) {
			b, _ := genBool.Get()
			genBool.Set(!b)
		})
	genBool.AddListener(binding.NewDataListener(func() {
		ss := generateStructDefinition(generateStructDefinitionInput{
			cGenDaoInput{
				selJsonCase.Selected,
				stdTime.Checked, gJsonSupport.Checked,
				descriptionTag.Checked, noJsonTag.Checked, noModelComment.Checked,
			},
			currentTbl,
			fs,
		})

		entry.SetText(ss)
	}))
	top := container.NewHBox(selJsonCase, gJsonSupport, stdTime,
		descriptionTag, noJsonTag, noModelComment)
	c := container.NewBorder(top, nil, nil, nil, entry)
	dataBrowser := container.NewTabItemWithIcon("", octicons.NoteIcon(),
		c)
	dataBrowser.Text = currentTbl + " struct"
	tabs.Append(dataBrowser)
	tabs.Select(dataBrowser)
}
